package com.fedorvlasov.lazylist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidstore.app.data.AppData;

public class LazyAdapter extends BaseAdapter {
    
    private Activity activity;
    private ArrayList<AppData> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public LazyAdapter(Activity a, ArrayList<AppData> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public static class ViewHolder{
        public TextView name;
        public TextView price;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.name=(TextView)vi.findViewById(R.id.name);
            holder.price=(TextView)vi.findViewById(R.id.price);
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        
        holder.name.setText(""+(position+1)+ "."+ data.get(position).AppName);
        if (data.get(position).Price > 0.0f){
        	holder.price.setText("$"+ data.get(position).Price);
        }
        else {
        	holder.price.setText("FREE");
        }
        holder.image.setTag(data.get(position).linkIcon);
        imageLoader.DisplayImage(data.get(position).linkIcon, activity, holder.image);
        return vi;
    }
}